package com.dante.mvp_login.presenter;

import android.os.Handler;

import com.dante.mvp_login.contract.UserContract;
import com.dante.mvp_login.model.UserModel;

/**
 * 类说明：
 * 作者：HD on 2017/11/17 17:28
 * 邮箱：hehandong@qq.com
 */

public class LoginPresenterImpl implements UserContract.LoginPresenter {

    private UserContract.LoginView mView;
    private final UserModel mUserModel;

    private Handler mHandler = new Handler();

    public LoginPresenterImpl(UserContract.LoginView view) {
        mView = view;
        mUserModel = new UserModel();
    }

    @Override
    public void registerUser(final String name, final String pwd) {
        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    Thread.sleep(2000);
                    final boolean onRegister = mUserModel.onRegister(name, pwd);
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            if (onRegister){
                                mView.onRegisterSucceed();
                            }else {
                                mView.onRegisterfailed();
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();


    }

    @Override
    public void loginUser(final String name, final String pwd) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                    final boolean onLogin = mUserModel.onLogin(name, pwd);
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            if (onLogin){
                                mView.onLoginSucceed();
                            }else {
                                mView.onLoginfailed();
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }
}
